<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Shops extends JS_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('shops_mdl');
		// if($this->session->suser->group != 1){
		// 	show_error('参数错误！');
		// }
	}

	//判断权限
	public function index(){
		if($this->session->suser->group == 1){
			$this->admin_index();
		}else{
			$this->user_index();
		}
	}

	// 管理员商铺首页
	private function admin_index(){

		$url = site_url('shops/index').'?';
		$this->db->start_cache();
		$key = $this->input->get('keywords',TRUE);
		if(!empty($key)){
			$this->db->like('name',$key);
			$url .= 'keywords='.$key.'&';
		}
		$this->db->stop_cache();

		$this->load->library('pagination');

        $config['base_url'] = substr($url,0,-1);
        $config['total_rows'] = $this->shops_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';
        $config['num_links'] = 3;

        $this->pagination->initialize($config);
        
        $page = $this->pagination->create_links();
        
        $list = $this->shops_mdl->select();
        $this->db->flush_cache();
        $list = $this->shops_mdl->assembly($list);

        $data = [
        	'page' => $page,
        	'list' => $list
        ];

		$this->_view('shops_admin',$data);
	}

	//用户商铺首页
	private function user_index(){
		//查询该用户下的所有店铺
		$this->load->model('users_mdl');
		$list_user = $this->users_mdl->get_lower($this->session->suser->user_id);
		$this->db->where('user_id',$this->session->suser->user_id);
		$list_user[] = $this->users_mdl->get_one();

		$map = array();
		foreach($list_user as $k => $v){
			$map[] = $v['user_id'];
		}

		$url = site_url('shops/index').'?';

		//--------------查询条件构建----------
		$this->db->start_cache();

		$this->db->where_in('uid',$map);

		$key = $this->input->get('keywords',TRUE);
		if(!empty($key)){
			$this->db->like('name',$key);
			$url .= 'keywords='.$key.'&';
		}
		$this->db->stop_cache();

		$this->load->library('pagination');

        $config['base_url'] = substr($url,0,-1);
        $config['total_rows'] = $this->shops_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';
        $config['num_links'] = 3;

        $this->pagination->initialize($config);
        
        $page = $this->pagination->create_links();
        
        $list = $this->shops_mdl->select();
        $this->db->flush_cache();
        $list = $this->shops_mdl->assembly($list);

        $data = [
        	'page' => $page,
        	'list' => $list
        ];
		$this->_view('shops_user',$data);

	}

	/**
	 * 商铺添加
	 */
	public function create(){
		if($this->session->suser->group != 1){
			show_404();
		}

		//查询单价金额
		$this->load->model('project_mdl');
		$this->db->where('id',1);
		$this->db->select('id,price');
		$project = $this->project_mdl->get_one();
		$data = [
			'price' => $project['price']
		];

		$this->_view('shops_create',$data);
	}

	/**
	 * 查询商铺信息
	 */
	public function get_shops(){
		if($this->session->suser->group != 1){
			show_404();
		}

		if(!$this->input->is_ajax_request()){
			show_404();
		}

		$sid = $this->input->post('sid',TRUE);
		$sid = intval($sid);

		if(empty($sid)){
			die(json_encode(['err'=>'参数错误！']));
		}

		$url = 'http://zbzmall.com/api/getshops?sid='.$sid.'&key='.$this->get_key();

		$res = $this->get_cunt($url);

		//判断数据是否存在
		if(empty($res)){
			die(json_encode(['err'=>'数据不存在']));
		}
		if(!empty($res->error)){
			die(json_encode(['err'=>$res->error]));
		}

		$this->db->where('id',$res->id);
		$res_is = $this->shops_mdl->get_one();
		if(!empty($res_is)){
			die(json_encode(['err'=>'该商铺已经存在']));
		}

		$this->load->model('users_mdl');
		$this->db->where('user_id',$res->founder);
		$res_user = $this->users_mdl->get_one();

		if(empty($res_user)){
			die(json_encode(['err'=>'该商铺用户数据异常，不能登入系统']));
		}

		if($res_user['group'] == 1){
			die(json_encode(['err'=>'该商铺不能登入系统']));
		}

		echo json_encode($res);
	}

	/**
	 * 修改商铺信息
	 */
	public function update(){
		if($this->session->suser->group != 1){
			show_404();
		}

		$sid = $this->input->get('sid',TRUE);
		$sid = intval($sid);
		if(empty($sid)){
			show_error('参数错误！');
		}

		//查询商铺数据
		$this->db->where('shops_id',$sid);
		$res = $this->shops_mdl->get_one();
		//print_r($res);
		$this->_view('shops_update',['res'=>$res]);
	}

	/**
	 * 修改商铺数据
	 */
	public function update_oper(){
		if($this->session->suser->group != 1){
			show_404();
		}

		if(!$this->input->is_ajax_request()){
			show_404();
		}

		$post = $this->input->post(NULL,TRUE);

		if(empty($post)){
			die('参数错误');
		}

		if(empty(intval($post['shops_id']))){
			die('商铺ID填写错误');
		}

		if(empty(intval($post['income']))){
			die('填写金额错误');
		}

		//判断商铺是否存在
		$shops_id = intval($post['shops_id']);
		$this->db->where('shops_id',$shops_id);
		$res_shops = $this->shops_mdl->get_one();
		if(empty($res_shops)){
			die('该商铺不存在');
		}

		$data = [ 
			'shops_id' => intval($post['shops_id']),
			'income' => intval($post['income']),
			'end_time' => strtotime($post['end_time']),  
			'name' => $post['name'],  
			'url' => $post['url'],  
			'uid' => $post['uid'],
			's_time' => $_SERVER['REQUEST_TIME']
		];

		$this->db->where('shops_id',intval($post['shops_id']));
		$res = $this->shops_mdl->update($data);

		if($res_shops['income'] != $post['income']){
			$this->load->model('bull_mdl');
			$this->db->where('shops_id',$shops_id);
			$this->bull_mdl->delete();

			$this->bull_mdl->distribution($res_shops['id'],'update');
		}	
		echo $res === true ? '1' : '更新数据失败！';
	}

	/**
	 * 设置店铺信息
	 */
	public function operation(){
		if($this->session->suser->group != 1){
			show_404();
		}

		if(!$this->input->is_ajax_request()){
			show_404();
		}

		$post = $this->input->post(NULL,TRUE);

		if(empty($post)){
			die('参数错误');
		}

		if(empty(intval($post['shops_id']))){
			die('商铺ID填写错误');
		}

		if(empty(intval($post['income']))){
			die('填写金额错误');
		}

		//判断商铺是否存在
		$shops_id = intval($post['shops_id']);
		$this->db->where('shops_id',$shops_id);
		$res_shops = $this->shops_mdl->get_one();
		if(!empty($res_shops)){
			die('该商铺已存在');
		}

		$data = [ 
			'shops_id' => intval($post['shops_id']),
			'income' => intval($post['income']),
			'end_time' => strtotime($post['end_time']),  
			'name' => $post['name'],  
			'url' => $post['url'],  
			'uid' => $post['uid'],
			's_time' => $_SERVER['REQUEST_TIME']
		];

		$res_shops = $this->shops_mdl->create($data);

		//结算金额（核心）
		if($res_shops === false){
			die('添加数据失败！');
		}
		$this->load->model('bull_mdl');
		$this->bull_mdl->distribution($this->db->insert_id());


		echo $res_shops === true ? '1' : '添加数据失败！';
	}

}

/* End of file Shops.php */
/* Location: ./application/controllers/Shops.php */